/**
 * zmn渠道管理-结算信息
 * 文件命名格式：zmn.模块.表/功能.js
 * @author xiangjunming
 * @since 2019/08/22 18:06
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;

    form.render(null, "zmn-form");

    // 自定义验证规则
    form.verify({
        nodeRequired: function () {
            let statementNode = $('input[name="statementNode"]:checked').val();
            if (statementNode == undefined || statementNode == "" || statementNode == null) {
                return "结算节点必选";
            }
        },
        channelRequired: function () {
            let statementChannel = $('input[name="statementChannel"]:checked').val();
            if (statementChannel == undefined || statementChannel == "" || statementChannel == null) {
                return "结算渠道必选";
            }
        },
        mergeRequired: function () {
            let mergeSettle = $('input[name="mergeSettle"]:checked').val();
            if (mergeSettle == undefined || mergeSettle == "" || mergeSettle == null) {
                return "合并结算必选";
            }
        },
        isEffectivePrice: function (value, item) {
            if (value != null && value != undefined && value != "") {
                if (!/^\d+(\.\d{1,2})?$/.test(value)) {
                    return "输入应为正数，最多两位小数";
                }

                if (value == '0' || value == '0.0' || value == '0.00') {
                    return "输入应为正数，最多两位小数";
                }

                let result = parseFloat(value);
                if (0 < result && result <= 100000) {
                } else {
                    return "最大值为1000000，请调整后再试";
                }
            }
        },
        isEffectivePercent: function (value, item) {
            if (value != null && value != undefined && value != "") {
                if (!/^\d+(\.\d{1,2})?$/.test(value)) {
                    return "获客成本比例范围为0~100";
                }

                let result = parseFloat(value);
                if (value == 100 || value == 100.0 || value == 100.00 || (0 <= value && value <= 100)) {
                } else {
                    return "获客成本比例范围为0~100";
                }
            }
        },
        telphone: function(value){
            if(value&&value.indexOf("*")==-1){
                if(value.length > 11){
                    return "电话号码长度不能超过11位";
                }
                if (!/^[0-9]+$/.test(value)) {
                    return "电话号码只能是数字";
                }
            }

        },
        bankAccount: function(value){
            if(value&&value.indexOf("*")==-1){
                if (!/^[0-9]+$/.test(value)) {
                    return "银行卡号码只能是数字";
                }
            }

        },
    });

    zmnLayUtil.layFormVerify(form);

    let lastVal = "";
    $("#financerPhone,#bankAccount,#alipayAccount,#wechatAccount").focus(function () {
        lastVal= $(this).val();
        if(lastVal.indexOf("*")!=-1){
            $(this).val(lastVal.substring(0,1));
        }
    });
    $("#financerPhone,#bankAccount,#alipayAccount,#wechatAccount").blur(function () {
        let val = $(this).val();
        if(val&&val==lastVal.substring(0,1)){
            $(this).val(lastVal);
        }
    });

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        var data = obj.field;
        var customerCost = data['customerCost'];
        var promotionServiceFee = data['promotionServiceFee'];
        var comprehensiveServiceFee = data['comprehensiveServiceFee'];
        if (customerCost != undefined && customerCost != null && customerCost != '') {
            data['customerCost'] = customerCost * 100;
        }
        if (promotionServiceFee != undefined && promotionServiceFee != null && promotionServiceFee != '') {
            data['promotionServiceFee'] = promotionServiceFee * 100;
        }
        if (comprehensiveServiceFee != undefined && comprehensiveServiceFee != null && comprehensiveServiceFee != '') {
            data['comprehensiveServiceFee'] = comprehensiveServiceFee * 100;
        }
        var url = "/channel/saveSettle.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success: function (resp) {
                var message = resp.status == 200 ? "操作成功！" : resp.message;
                layer.msg(message);
                if (resp.status == 200) {
                    // zmnPage.direct("/channel/settle.action?channelId=" + resp.id + "&addFlag=" + json.addFlag, 0);
                    zmnPage.reload(resp.message, 1000);
                }
            },
            error: function (XMLHttpRequest, status, errorThrown) {
            }
        });
        return false;
    });

    form.on('radio(customerCostMode)', function (data) {
        let value = data.value;
        doRenderDiv(value);
    });

    form.on('select(statementMode)', function (data) {
        let value = data.value;
        renderOption(value);
    });

    function renderOption(value, selectedSGT, selectedSET, selectedSGTS, selectedSETS) {
        function _renderOption(arr, selectedValue, $selectId) {
            let optionHtml = '';
            arr.forEach(item => {
                optionHtml += `<option value="${item.value}" ${item.value == selectedValue ? 'selected' : ''}>${item.text}</option>`;
            })
            $selectId.html(optionHtml);
        }

        if (value == 2) {
            _renderOption(json.weeks, selectedSGT != null ? selectedSGT : 1, $('#statementGenerateTime'));
            _renderOption(json.weeks, selectedSET != null ? selectedSET : 1, $('#statementEndTime'));
        }

        if (value == 5) {
            _renderOption(json.days, selectedSGT ? selectedSGT : 1, $('#statementGenerateTime'));
            _renderOption(json.days, selectedSET ? selectedSET : 28, $('#statementEndTime'));
        }

        if (value == 4) {
            _renderOption(json.days, selectedSGT ? selectedSGT : 1, $('#statementGenerateTime'));
            _renderOption(json.days, selectedSET ? selectedSET : 1, $('#statementEndTime'));

            _renderOption(json.days, selectedSGTS ? selectedSGTS : 16, $('#statementGenerateTimeSecond'));
            _renderOption(json.days, selectedSETS ? selectedSETS : 16, $('#statementEndTimeSecond'));

            $('#statementGenerateTimeSecondDiv').show();
            $('#statementEndTimeSecondDiv').show();
        } else {
            $('#statementGenerateTimeSecondDiv').hide();
            $('#statementEndTimeSecondDiv').hide();
        }
        form.render(null, "zmn-form");
    }

    function doRenderDiv(value) {
        // 按固定金额
        if (value == 1) {
            $("#customerCostDiv").hide();
            $("#customerCostPercentDiv").hide();
            $("#customerCostPercent").val("");
            $("#customerCost").val("");
        } else if (value == 3) {
            $("#customerCostDiv").hide();
            $("#customerCost").val("");
            $("#customerCostPercentDiv").show();
        } else if (value == 2) {
            $("#customerCostDiv").show();
            $("#customerCostPercentDiv").hide();
            $("#customerCostPercent").val("");
        }
        form.render(null, "zmn-form");
    }

    if (json.channelSettle.statementMode) {
        renderOption(json.channelSettle.statementMode, json.channelSettle.statementGenerateTime, json.channelSettle.statementEndTime,
            json.channelSettle.statementGenerateTimeSecond, json.channelSettle.statementEndTimeSecond);
    }

    //对外暴露的接口
    exports('channel_settle', {});
});